/*
 * @Author: xiaosihan
 * @Date: 2024-07-23 21:44:51
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-17 16:48:19
 */

import { observable, configure } from "mobx";
import partsApi from "@api/partsApi";
import { find, get } from "lodash";
import { PartData } from "@type";
configure({ enforceActions: "never" });

const partsLibraryStore = observable(
  {
    // 类目
    category: "",
    setCategory(category: string) {
      this.category = category;
      this.getPartList();
    },

    //零件数据列表
    partList: [] as Array<PartData>,
    setPartList(partList: Array<PartData>) {
      this.partList = partList;
    },

    //翻页
    page: 1,
    pageSize: 10,
    totalCount: 0,
    setPage(page: number) {
      this.page = page;
      this.getPartList();
    },

    //获取零件列表
    async getPartList() {
      const { page, pageSize, category } = this;
      const modalTypeResult = await partsApi.getModelType();
      const modalTypeList = get(modalTypeResult, ["data", "data"]);
      let data: any = {};
      if (category === "collaborate") {
        Object.assign(data, { shared: true });
      } else if (category === "2d") {
        const typeId = get(
          find(modalTypeList, (d) => d.name === "工程图纸"),
          ["id"]
        );
        Object.assign(data, { typeId: typeId, shared: false });
      } else if (category === "3d") {
        const typeId = get(
          find(modalTypeList, (d) => d.name === "3D模型"),
          ["id"]
        );
        Object.assign(data, { typeId: typeId, shared: false });
      }
      const result = await partsApi.get_list(page, pageSize, data);
      const list = get(result, ["data", "data"]);
      const totalCount = get(result, ["data", "totalCount"]);
      this.partList = list;
      this.totalCount = Number(totalCount);
    },
  },
  {},
  { deep: false }
);

export default partsLibraryStore;
